﻿using System;
using System.Diagnostics.Contracts;
using Isuka.Iteration;

namespace Isuka.Excel.Iteration
{
    public class XRangeLinerIterable
        : IIterable<XRangeLinerIterator>
    {
        readonly XRange range;
        readonly int? end;
        readonly XDirection direction;

        public XRangeLinerIterable(XRange begin, XRange end, XDirection direction)
        {
            Contract.Requires<ArgumentNullException>(begin != null, "begin");

            if (end == null)
            {
                this.range = begin;
                this.end = null;
            }
            else
            {
                this.range = begin.To(end);
                this.end = direction.Distance(begin, end);
            }
            this.direction = direction;
        }

        public XRangeLinerIterable(XRange range, int? end, XDirection direction)
        {
            Contract.Requires<ArgumentNullException>(range != null, "range");

            this.range = range;
            this.end = end;
            this.direction = direction;
        }

        #region IIterable<XRangeLinerIterator> メンバー

        public XRangeLinerIterator Begin()
        {
            return new XRangeLinerIterator(range, direction, 0);
        }

        public XRangeLinerIterator End()
        {
            return end.HasValue ? new XRangeLinerIterator(range, direction, end.Value) : null;
        }

        #endregion
    }
}
